package bb.lanxing.calc.notify;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;

public class AlertControlDelegate implements SoundPool.OnLoadCompleteListener, AudioManager.OnAudioFocusChangeListener {
    private static final int INVALID_PLAY_ID = 0;
    private AudioManager audioManager;
    private Context context;
    private boolean loaded;
    private boolean shouldBeAlert;
    private int soundIdLoaded;
    private SoundPool soundPool;
    private int soundRawId;
    private int playId = 0;

    private Runnable alertRunnable = () -> {
        if (AlertControlDelegate.this.shouldBeAlert) {
            AlertControlDelegate alertControlDelegate = AlertControlDelegate.this;
            alertControlDelegate.playId = alertControlDelegate.soundPool
                    .play(AlertControlDelegate.this.soundIdLoaded, 1.0f, 1.0f, 0, 0, 1.0f);
            AlertControlDelegate.this.alertHandler.postDelayed(AlertControlDelegate.this.alertRunnable, 1000L);
        }
    };
    private Handler alertHandler = new Handler(Looper.getMainLooper());

    public AlertControlDelegate(Context context, int soundRawId) {
        this.context = context;
        this.soundRawId = soundRawId;
    }

    private void initAudio() {
        synchronized (this) {
            if (this.soundPool != null) {
                return;
            }
            if (Build.VERSION.SDK_INT >= 21) {
                this.soundPool = new SoundPool.Builder()
                        .setAudioAttributes(new AudioAttributes.Builder()
                                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                                .setLegacyStreamType(3)
                                .setUsage(AudioAttributes.USAGE_MEDIA)
                                .build())
                        .build();
            } else {
                this.soundPool = new SoundPool(3, 3, 100);
            }
            this.soundPool.setOnLoadCompleteListener(this);
            this.audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
            this.soundIdLoaded = this.soundPool.load(this.context, this.soundRawId, 1);
        }
    }

    public void play() {
        this.shouldBeAlert = true;
        playInternal();
    }

    private void playInternal() {
        initAudio();
        if (this.loaded && this.shouldBeAlert && this.audioManager.requestAudioFocus(this, 3, 3) == 1) {
            this.alertHandler.post(this.alertRunnable);
        }
    }

    public void stop() {
        this.shouldBeAlert = false;
        this.alertHandler.removeCallbacks(this.alertRunnable);
        this.playId = 0;
        AudioManager audioManager = this.audioManager;
        if (audioManager != null) {
            audioManager.abandonAudioFocus(this);
        }
    }

    public void release() {
        stop();
        SoundPool soundPool = this.soundPool;
        if (soundPool != null) {
            soundPool.release();
        }
        this.alertHandler = null;
        this.audioManager = null;
        this.loaded = false;
        this.soundPool = null;
    }

    public boolean isPlaying() {
        return (this.soundPool == null || this.playId == 0) ? false : true;
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
        boolean z = this.soundIdLoaded == sampleId && status == 0;
        this.loaded = z;
        if (z) {
            playInternal();
        }
    }

    @Override
    public void onAudioFocusChange(int focusChange) {
        if (focusChange == 1 || focusChange == 2 || focusChange == 3 || focusChange == 4) {
            return;
        }
        stop();
    }
}
